<?php
/* P3(c)

******************************************************************************
                                                                                      
                         __                                                        
                        /__)                                                       
                       /__        P3 - moving Technologie            
                        __)                                                          
                        __)                                                          
                                                                                        
                                                                                        
       Copyright 2009 by p3.co.at                                                                                 
                                                                                        
       visit www.p3.co.at to get more Informations 
       about Teriko - Betriebsdatenerfassung                                                                                 
       - it's not allowed to change this scripts
       - it's not allowed to remove this copyright-tag

       -- you can download the complete license   
          under http://www.teriko.org

 (c) by Patrik Pfaffenbauer and P3 (http://www.p3.co.at)

 | Programname: P3.Teriko
 | Authtor: P3.Verein
 | Coder: Patrik Pfaffenbauer
 | Version: 0.1

 | Change Index
 |_____________________________________________
 |                                             |
 |23.03.2010 Erstellung | Patrik Pfaffenbauer  |
 |_____________________________________________|

******************************************************************************

P3(c) */
class Database
{
	var $queryCount;
	var $fetchCount;
	
	public function Database()
	{
		include("_mysql.php");
	}
	
	function SaveObject($table)
	{
		//Reading Table Schema
		$obj = $this->Query("SHOW COLUMNS FROM ".$table);
		$i = $obj->num_rows;
		$count = 0;
		
		//Running threw table schema
		while($line = $this->Fetch($obj))
		{
			//Ignore every auto increment field
			if($line->Extra == "auto_increment")
			{	
				$count++;
				continue;
			}
			
			//if an POST variable from an table field exist
			if($_POST[$line->Field] != "")
			{
				//Adding the field to the sql query
				$keys .= $line->Field.", ";
				$values .= "'".$_POST[$line->Field]."', ";
			}
			else
			{
			
			}
			$count++;
			
		}
		//remove the last ', '
		$keys = substr($keys, 0, -2); 
		$values = substr($values, 0, -2); 
		
		//building the query
		$query = "INSERT INTO ".$table." (".$keys.") VALUES (".$values.")";
		
		//executing the query
		$this->Query($query);
	}
	
	function SearchObject($table, $keysOnly=false)
	{
		//Reading Table Schema
		$obj = $this->Query("SHOW COLUMNS FROM ".$table);
		$i = $obj->num_rows;
		$count = 0;
		
		//Running threw table schema
		while($line = $this->Fetch($obj))
		{
			//Ignore every auto increment field
			if($line->Extra == "auto_increment")
			{	
				$count++;
				continue;
			}
			
			//if an POST variable from an table field exist
			if($_POST[$line->Field] != "")
			{
				//Adding the field to the sql query
				$keys .= $line->Field.", ";
				$values .= $line->Field." LIKE '".$_POST[$line->Field]."' AND ";
			}
			else
			{
			
			}
			$count++;
			
		}
		//remove the last ', '
		$values = substr($values, 0, -4); 
		
		//building the query
		$query = "SELECT * FROM ".$table." WHERE ".$values;
		
		//executing the query
		if($keysOnly)
			return $values;
			
		return $query;
	}
	
	function UpdateObject($table)
	{
		if($_POST['id'] == "")
		{
			Alert("Update without ID not possible");
			return;
		}
		//Reading Table Schema
		$obj = $this->Query("SHOW COLUMNS FROM ".$table);
		$i = $obj->num_rows;
		$count = 0;
		
		//Running threw table schema
		while($line = $this->Fetch($obj))
		{
			//Ignore every auto increment field
			if($line->Extra == "auto_increment")
			{	
				$count++;
				continue;
			}
			
			//if an POST variable from an table field exist
			if($_POST[$line->Field] != "")
			{
				//Adding the fields and values to the sql query
				$keys .= $line->Field." = '".$_POST[$line->Field]."', ";
			}
			$count++;
			
		}
		//remove the last ', '
		$keys = substr($keys, 0, -2); 
		
		//building the query
		$query = "UPDATE ".$table." SET ".$keys." WHERE id=".$_POST['id'];
		//executing the query
		$this->Query($query);
	}	
	function Delete($table)
	{
		$id = $_POST['id'];
		
		$query = "DELETE FROM ".$table." WHERE id=".$id;
		
		$this->Query($query);	
	}
	
	function Query($query)
	{
		include("./_mysql.php");
		$objCon = new mysqli($db, $dbuser, $dbpassword);
		$objCon->Select_db($dbname);
		$this->queryCount++;
	
		if($result = $objCon->query($query))
		{
			$objCon->Close();
			return $result;
		}
		else
		{
			$objCon->Close();
			
		}
	}
	
	function FieldName($result, $index)
	{
	 	$finfo = $result->fetch_fields();
		$i = 0;
		
	    foreach ($finfo as $val) 
	    {
	     	if($i == $index)
	     		return $val->name;
	
	     	$i++;
	    }	
	}
	function FieldType($result, $index)
	{
		$finfo = $result->fetch_fields();
		$i = 0;
		
	    foreach ($finfo as $val) 
	    {
	     	if($i == $index)
	     		return $val->type;
	
	     	$i++;
	    }
	}
	
	function FetchArray($result)
	{
		if($line = $result->fetch_array())
		{
			return $line;
		}
		else
		{
			
		}
	}
	function FetchAssoc($result)
	{
		if($line = $result->fetch_assoc())
		{
			return $line;
		}
		else
		{
			
		}
	}
	function FetchRow($result)
	{
		if($line = $result->fetch_row())
		{
			return $line;
		}
		else
		{
			
		}
	}
	function Fetch($result)
	{
		if($line = $result->fetch_object())
		{
			return $line;
		}
		else
		{
			
		}
	}
	
	function NumRow($res)
	{
		if($res->num_rows)
			return true;
		else
			return false;
	}
	function GetValue($key)
	{
		include("./_tables.php");
		$res = $this->Query("SELECT * FROM ".$tblsettings." WHERE `key` ='".$key."';");
		
		if($res->num_rows)
		{
			$line = $this->Fetch($res);
			return $line->value;
		}
		else
		{
			return "key not found";
		}
	}
	function GetCount($table, $where)
	{
		$query = "SELECT COUNT(*) as a FROM ".$table." ".$where;
		
		$res = $this->Fetch($this->Query($query));
		
		return $res->a;
	}
	function GetSqlBool($value)
	{
		return ($value == "true") ?  1 :  0;
		if($value == "true")
		{
			return 1;
		}
		else
		{
			return 0;
		}
	}
	function GetMysqlArray($result)
	{
		while($line = $this->FetchArray($result))
		{
			$value[] = $line;
		}
		
		return $value;
	}
	
}

?>